<?php
/*
* Kusht Mud.
* ----
* The MIT License
* Copyright (c) 2010 Andrew Tutt
* ----
*/

/* Affect Class
*
*  Affect feels like more of a struct than a class. 
*  It is mostly a container for information about what a particular affect is.
*/

class Affect {
	public $name;			// A unique string name for the affect, for ease of humans. (Like, "thief sneak" vs. "sneaky item");
	public $type;			// Type is a key, allowing us to link the affect to something (like a skill)
	public $stat_modified;	// obvious. See kushtVars for the list
	public $modifier;		// And the amount.
	public $flag; 			// What is it's boolean flag? (SNEAK) for instance
	
	// We use time keys because of the monotonic nature of time, 
	// and the efficiency that produces
	public $affected;		// An array of affected users
	
	function __construct() {
		$this->affected = array();
	}
	
	// Recursive timeout expiration
	public function expire() {
		if(!current($this->affected)) { return; } // Nothing in Array
		
		$t = microtime(1);
		$e = key($this->affected);
		if ($t > $e) { // First in is beyond the expire time so remove it
			$this->unaffect(current($this->affected));
			array_shift($this->affected);
			$this->expire();
		}
	}
	
	public function affect($object,$duration) {
		$object->set_bit($this->flag);
		if($duration == INFINITE) { return; }
		
		$this->affected += array(microtime(1)+$duration, $object);
		ksort($this->affected); // Earliest expiration first
		World::sortAffects();
	}
	
	public function unaffect($char) {
		$char->rmv_bit($this->flag);
	}
	
	// Forced removal (disruptive)
	public function disrupt_affect($char) {
		foreach($this->affected as $k=>$c) {
			if($c==$char) {
				$this->unaffect($char);
				unset($this->affected[$k]);
				return;
			}
		}
	}
}